<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Orders_Model extends CI_Model{
    function __construct()
    {
        parent::__construct();
    }
    function getAllData($num,$offset){
        $query = $this->db->get('orders',$num,$offset);
      
        return $query->result();  
    } 
      function get_object($id)
    {
		$query = $this->db->get_where('orders', array('order_id' =>$id));
		$this->db->select('orders.*,city.name as city_name,district.name as district_name');
		$this->db->select('COALESCE(SUM(aw_order_product.total),0) as total_order',false) ;
		$this->db->join('city','city.city_id=orders.city_id');
		$this->db->join('district','orders.district_id=district.district_id');
		$this->db->join('order_product','orders.order_id=order_product.order_id','left');
		$this->db->where('orders.order_id',$id);
		$this->db->group_by("order_product.order_id"); 
		$query = $this->db->get('orders');
		return $query->row();
    }
    function getNumData(){
        $query = $this->db->get('orders');
        return $query->num_rows();
    }
    function delete($id){
          $this->db->where('order_id',$id);
          $query = $this->db->delete('orders');
          if($query){
              return true;
          }else{
              return false;
          }
    }
	function deleted($id){
          $this->db->where('order_id',$id);
		  $data=array('bl_active'=>'-1');
          $query = $this->db->update('orders',$data);
          if($query){
              return true;
          }else{
              return false;
          }
    }
	function update_order($order_id,$data){
          $query = $this->db->update('orders',$data, "order_id = $order_id");
          if($query){
              return true;
          }else{
              return false;
          }
    }
    function get_list_permit(){
        return $this->db->get('permission')->result();
    }
   
   function saveData($order_id,$data){
             if($order_id!=0){
                    $this->db->where('order_id',$order_id);
                    if($this->db->update('orders',$data)){
                         return true;
                    }else{
                         return false;
                    }
             }else{
                    if($this->db->insert('orders',$data)){
						
                        return $this->db->insert_id();
                        
                    }else{
                        return false;
                    }
            }
    }
    function get_num_data($arr_search){
            /*Begin search*/
          	if(!empty($arr_search)){
			   foreach($arr_search as $key=>$value){
				   
				   if($value!='')
				 	$this->db->where("orders.$key",$value);  
			   }
		   }
            /*End search*/
			$this->db->join('city','city.city_id=orders.city_id');
			$this->db->join('district','orders.district_id=district.district_id');
			$this->db->join('order_product','orders.order_id=order_product.order_id','left');
			$this->db->group_by("order_product.order_id"); 
            $this->db->order_by('orders.dt_create','DESC');
            $query = $this->db->get('orders');
			return $query->num_rows();
    }
    function get_all_data($num,$offset,$arr_search)
    {
           $this->db->select('orders.*,city.name as city_name,district.name as district_name');
		   $this->db->select('COALESCE(SUM(aw_order_product.total),0) as total_order',false) ;
		   if(!empty($arr_search)){
			   foreach($arr_search as $key=>$value){
				 if($value!='')
				 	$this->db->where("orders.$key",$value);  
			   }
		   }
			$this->db->join('city','city.city_id=orders.city_id');
			$this->db->join('district','orders.district_id=district.district_id');
			$this->db->join('order_product','orders.order_id=order_product.order_id','left');
			$this->db->group_by("order_product.order_id"); 
            $this->db->order_by('orders.order_id','DESC');
            $query = $this->db->get('orders',$num,$offset);
             return $query->result();
    }
    function getCategoryName($partners_id=0) {
        $this->db->select("name");
        $this->db->where('partners_id',$partners_id);
        $query = $this->db->get("partners");
        return $query->row();
    }
	function export_order($arr_search)
    {
	$this->db->select('orders.*,products.name,order_product.quantity,order_product.price,order_product.total,district.name as district_name,district.type as district_type,city.name as city_name, ward.name as ward_name,ward.type as ward_type, color.name as color_name, size.name as size_name');
	if(!empty($arr_search)){
			   foreach($arr_search as $key=>$value){
				 if($key=='tungay' && $value!='')
				 	$this->db->where('orders.dt_create >=', $value);
				 else if($key=='denngay' && $value!='')
					$this->db->where('orders.dt_create <= ', $value);
				 else
				 if($value!='')
				 	$this->db->where("orders.$key",$value);  
			   }
		   }
		   $this->db->join('order_product','orders.order_id=order_product.order_id');
		   $this->db->join('city','orders.city_id=city.city_id');
		   $this->db->join('district','orders.district_id=district.district_id');
		   $this->db->join('ward','orders.ward_id=ward.ward_id');
		   $this->db->join('products','products.products_id=order_product.product_id');
		   $this->db->join('color','color.color_id=order_product.color');
		   $this->db->join('size','size.size_id=order_product.size');
		   
             $this->db->order_by('dt_create','DESC');
             $query = $this->db->get('orders');
             return $query->result();
    }
    
    
  }
?>
